/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web.vote;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.vote.VoteResult;
import com.thinkgem.jeesite.modules.sys.service.vote.VoteResultService;

/**
 * 投票结果类Controller
 * @author 郑学兵
 * @version 2017-05-22
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/vote/voteResult")
public class VoteResultController extends BaseController {

	@Autowired
	private VoteResultService voteResultService;
	
	@ModelAttribute
	public VoteResult get(@RequestParam(required=false) String id) {
		VoteResult entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = voteResultService.get(id);
		}
		if (entity == null){
			entity = new VoteResult();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:vote:voteResult:view")
	@RequestMapping(value = {"list", ""})
	public String list(VoteResult voteResult, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VoteResult> page = voteResultService.findPage(new Page<VoteResult>(request, response), voteResult); 
		model.addAttribute("page", page);
		return "modules/sys/vote/voteResultList";
	}

	@RequiresPermissions("sys:vote:voteResult:view")
	@RequestMapping(value = "form")
	public String form(VoteResult voteResult, Model model) {
		model.addAttribute("voteResult", voteResult);
		return "modules/sys/vote/voteResultForm";
	}

	@RequiresPermissions("sys:vote:voteResult:edit")
	@RequestMapping(value = "save")
	public String save(VoteResult voteResult, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, voteResult)){
			return form(voteResult, model);
		}
		voteResultService.save(voteResult);
		addMessage(redirectAttributes, "保存投票结果类成功");
		return "redirect:"+Global.getAdminPath()+"/sys/vote/voteResult/?repage";
	}
	
	@RequiresPermissions("sys:vote:voteResult:edit")
	@RequestMapping(value = "delete")
	public String delete(VoteResult voteResult, RedirectAttributes redirectAttributes) {
		voteResultService.delete(voteResult);
		addMessage(redirectAttributes, "删除投票结果类成功");
		return "redirect:"+Global.getAdminPath()+"/sys/vote/voteResult/?repage";
	}

}